package com.lxl.xm.controller;

import com.lxl.xm.common.BaseResponse;
import com.lxl.xm.common.ErrorCode;
import com.lxl.xm.common.ResultUtils;
import com.lxl.xm.exception.BusinessException;
import com.lxl.xm.model.dto.student_branch.StudentCompleteBranchRequest;
import com.lxl.xm.model.dto.student_branch.StudentJoinBranchRequest;
import com.lxl.xm.model.dto.task_branch.TaskBranchAddRequest;
import com.lxl.xm.model.entity.TaskBranch;
import com.lxl.xm.service.StudentTaskService;
import com.lxl.xm.service.TaskBranchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 支线任务
 */
@RestController
@RequestMapping("/taskBranch")
@Slf4j
public class TaskBranchController {

    @Resource
    TaskBranchService taskBranchService;

    @Resource
    StudentTaskService studentTaskService;

    /**
     * 添加支线任务
     * @param taskBranchAddRequest
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<String> addBranchTask(@RequestBody TaskBranchAddRequest taskBranchAddRequest){
        if (taskBranchAddRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"分支任务信息为空");
        }
        taskBranchService.addBranchTask(taskBranchAddRequest);
        return ResultUtils.success("添加支线任务成功");
    }


    /**
     * 学生参加支线任务 需要学生id，任务id，任务子id
     * @param studentJoinBranchRequest
     * @return
     */
    @PostMapping("/join")
    public BaseResponse<Boolean> studentJoinBranchTask(@RequestBody StudentJoinBranchRequest studentJoinBranchRequest){
        if (studentJoinBranchRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"学生参加分支任务请求信息错误");
        }
        //学生有没有参加主线任务并且完成主线任务
        Boolean studentJoinTaskAndCompleted
                = studentTaskService.isStudentJoinTaskAndCompleted(studentJoinBranchRequest.getStudentId(),
                studentJoinBranchRequest.getTaskId());
        //如果完成了
        if (studentJoinTaskAndCompleted){
            //参加支线任务
            //todo 检查支线任务是否在当前主线任务当中
            return ResultUtils.success(taskBranchService.studentJoinTaskBranch(studentJoinBranchRequest));
        }else {
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"学生还未参加主线任务");
        }
    }

    /**
     * 学生完成支线任务
     * @param studentCompleteBranchRequest
     * @return
     */
    @PostMapping("/complete")
    public BaseResponse<String> studentCompleteBranchTask
            (@RequestBody StudentCompleteBranchRequest studentCompleteBranchRequest){
        if (studentCompleteBranchRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数错误");
        }
        taskBranchService.studentCompleteBranchTask(studentCompleteBranchRequest);
        return ResultUtils.success("任务完成");
    }



}
